﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class ElevatorMasterInfoController : BaseController
    {
        public ElevatorInfoApp _ElevatorInfoApp;

        public ElevatorMasterInfoController(ElevatorInfoApp elevatorInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            _ElevatorInfoApp = elevatorInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult ElevatorMasterInfoAsync()
        {
            return View();
        }
        public  async Task<JsonResult> GetElevatorMasterInfosAsync()
        {
            ElevatorMasterInfoDto elevatorMasterInfoDto = new();

            elevatorMasterInfoDto.PageIndex = (Start / Length) + 1;
            elevatorMasterInfoDto.PageSize = Length;
            elevatorMasterInfoDto.CrossroadEquipmentCode = Request.Form["CrossroadEquipmentCode"];
            var elevatorinfos = await _ElevatorInfoApp.GetElevatorMasterInfosAsync(elevatorMasterInfoDto);

            var pageData = new TableData<ElevatorMasterInfoDto>(Draw, elevatorinfos.Total, elevatorinfos.Total, elevatorinfos.Result);
            //await AddTracesInfo(5, 0, "获取堆垛机作业模式数据");
            return Json(pageData);
        }

        public async Task<JsonResult> ModifyEleStatusAsync()
        {
            var id = Request.Form["id"].ToInt64();
            var isEnable = Request.Form["isEnable"].ToBool();
            bool result;
            string msg = "禁用";
            try
            {


                var elevatorInfoDto = await _ElevatorInfoApp.GetElevatorMasterInfoByIDAsync(id);
                if (isEnable)
                {
                    msg = "启用";
                }
                if (elevatorInfoDto == null)
                {
                    result = false;
                    msg += "查询失败";
                    return CreateJsonGetResult(result, msg);
                }
                elevatorInfoDto.IsEnable = !isEnable;
                try
                {
                    result = await _ElevatorInfoApp.UpdateElevatorMasterAsync(elevatorInfoDto);
                    msg += "成功!";
                }
                catch (Exception e)
                {
                    result = false;
                    msg = e.Message;
                }
            }
            catch (Exception e)
            {

                throw;
            }
            return CreateJsonGetResult(result, msg);
            //}
        }


        /// <summary>
        /// 添加或者编辑提升机十字口配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> ElevatorMasterInfoAddOrEditAsync(long id)
        {
            var elevatorInfoDto = new ElevatorMasterInfoDto();
            if (id > 0)
            {
                elevatorInfoDto = await _ElevatorInfoApp.GetElevatorMasterInfoByIDAsync(id);
            }
            return View(elevatorInfoDto);
        }

        /// <summary>
        /// 编辑或者新增数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateElevatorMasterInfoAsync()
        {
            var elevatorInfoDto = new ElevatorMasterInfoDto();
            elevatorInfoDto.ID = Request.Form["id"].ToInt64();
            elevatorInfoDto.CrossroadEquipmentCode = Request.Form["CrossroadEquipmentCode"].ToString();
            elevatorInfoDto.ElevatorCode = Request.Form["ElevatorCode"].ToString();
            elevatorInfoDto.NextElevatorCodes = Request.Form["NextElevatorCodes"].ToString();
            elevatorInfoDto.ElevatorPUCode = Request.Form["ElevatorPUCode"].ToString();
            elevatorInfoDto.ElevatorDOCodes = Request.Form["ElevatorDOCodes"].ToString();
            elevatorInfoDto.CanToEquipments = Request.Form["CanToEquipments"].ToString();
            elevatorInfoDto.IsEnable = Request.Form["IsEnable"].ToBool();

            bool result;
            string msg;
            try
            {
                if (elevatorInfoDto.ID == 0)
                {
                    var res = await _ElevatorInfoApp.InsertElevatorMasterAsync(elevatorInfoDto);
                    await AddTracesInfo(2, 0, "新增提升机十字口配置");
                    result = res;
                }
                else
                {
                    result = await _ElevatorInfoApp.UpdateElevatorMasterAsync(elevatorInfoDto);
                    await AddTracesInfo(3, 0, "修改提升机十字口配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }


        //public async Task<JsonResult> ModifyEleUseAsync()
        //{
        //    var id = Request.Form["id"].ToInt64();
        //    var isNextEnd = Request.Form["isNextEnd"].ToBool();
        //    bool result;
        //    string msg = "设为终结点";
        //    try
        //    {


        //        var elevatorInfoDto = await _ElevatorInfoApp.GetElevatorMasterInfoByIDAsync(id);
        //        if (isNextEnd)
        //        {
        //            msg = "设为终结点";
        //        }
        //        if (elevatorInfoDto == null)
        //        {
        //            result = false;
        //            msg += "查询失败";
        //            return CreateJsonGetResult(result, msg);
        //        }
        //        elevatorInfoDto.IsNextEnd = !isNextEnd;
        //        try
        //        {
        //            result = await _ElevatorInfoApp.UpdateElevatorMasterAsync(elevatorInfoDto);
        //            msg += "成功!";
        //        }
        //        catch (Exception e)
        //        {
        //            result = false;
        //            msg = e.Message;
        //        }
        //    }
        //    catch (Exception e)
        //    {

        //        throw;
        //    }
        //    return CreateJsonGetResult(result, msg);
        //    //}
        //}
    }
}
